//
//  GoodsListDoubleCell.m
//  TerritoryShopkeeper
//
//  Created by xiaowu on 2017/12/1.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "GoodsListDoubleCell.h"

@interface GoodsListDoubleCell ()

@property (nonatomic, strong) GoodsListDoubleItemView *firstView;
@property (nonatomic, strong) GoodsListDoubleItemView *secondView;

@property (nonatomic, strong) GoodsListModel *firstModel;
@property (nonatomic, strong) GoodsListModel *secondModel;

@end

@implementation GoodsListDoubleCell

+ (NSArray *)getCellItemArr:(NSArray *)dataArr withIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *arr = [NSMutableArray array];
    
    GoodsListModel *firstModel = [dataArr safeObjectAtIndex:(2 * indexPath.row)];
    [arr addObject:firstModel];
    
    if ((dataArr.count - 1) / 2 == indexPath.row) {
        if (dataArr.count % 2 == 0) {
            GoodsListModel *secondModel = [dataArr safeObjectAtIndex:(2 * indexPath.row) + 1];
            [arr addObject:secondModel];
        }
    } else {
        GoodsListModel *secondModel = [dataArr safeObjectAtIndex:(2 * indexPath.row) + 1];
        [arr addObject:secondModel];
    }
    return arr;
}

// 获取一行俩个的数组
+ (NSArray *)getCellItemArr:(NSArray *)dataArr withRow:(NSInteger)row {
    NSMutableArray *arr = [NSMutableArray array];
    
    GoodsListModel *firstModel = [dataArr safeObjectAtIndex:(2 * row)];
    [arr addObject:firstModel];
    
    if ((dataArr.count - 1) / 2 == row) {
        if (dataArr.count % 2 == 0) {
            GoodsListModel *secondModel = [dataArr safeObjectAtIndex:(2 * row) + 1];
            [arr addObject:secondModel];
        }
    } else {
        GoodsListModel *secondModel = [dataArr safeObjectAtIndex:(2 * row) + 1];
        [arr addObject:secondModel];
    }
    return arr;
}

+ (NSInteger)getRowNumberWithItemArr:(NSArray *)dataArr {
    return ((dataArr.count + 1) / 2);
}

+ (CGFloat)getHeight {
    return [GoodsListDoubleItemView getHeight];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self configure];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat width = ScreenWidth * 0.5 - 0.5;
    self.firstView.frame = CGRectMake(0, 0, width, [GoodsListDoubleItemView getHeight]);
    self.secondView.frame = CGRectMake(self.firstView.right + 1, 0, width, [GoodsListDoubleItemView getHeight]);
    
}

#pragma mark - Method
- (void)configure {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [self.contentView addSubview:self.firstView];
    [self.contentView addSubview:self.secondView];
    
    [self responseBlock];
}

- (void)responseBlock {
    __weak typeof(self) weakSelf = self;
    self.firstView.block = ^{
        if (weakSelf.block) {
            weakSelf.block(0);
        }
    };
    self.secondView.block = ^{
        if (weakSelf.block) {
            weakSelf.block(1);
        }
    };
}

- (void)setValuesForCellWithItems:(NSArray *)arr {
    self.secondView.hidden = ((arr.count % 2) == 1);
    
    self.firstModel = [arr firstObject];
    self.secondModel = [arr lastObject];
    
    [self.firstView setItemViewWithItem:self.firstModel];
    [self.secondView setItemViewWithItem:self.secondModel];

}

#pragma mark - Getters
- (GoodsListDoubleItemView *)firstView {
    if (!_firstView) {
        _firstView = [[GoodsListDoubleItemView alloc] init];
    }
    return _firstView;
}

- (GoodsListDoubleItemView *)secondView {
    if (!_secondView) {
        _secondView = [[GoodsListDoubleItemView alloc] init];
    }
    return _secondView;
}

@end
